package com.ruoyi.zcxt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zcxt.constant.ZcxtConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zcxt.domain.ZcLocation;
import com.ruoyi.zcxt.service.IZcLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 资产位置（树）Controller
 * 
 * @author ruoyi
 * @date 2024-01-06
 */
@RestController
@RequestMapping("/zcxt/location")
public class ZcLocationController extends BaseController
{
    @Autowired
    private IZcLocationService zcLocationService;

    /**
     * 查询资产位置（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:list')")
    @GetMapping("/list")
    public AjaxResult list(ZcLocation zcLocation)
    {
        List<ZcLocation> list = zcLocationService.selectZcLocationList(zcLocation);
        return success(list);
    }

    /**
     * 导出资产位置（树）列表
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:export')")
    @Log(title = "资产位置（树）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcLocation zcLocation)
    {
        List<ZcLocation> list = zcLocationService.selectZcLocationList(zcLocation);
        ExcelUtil<ZcLocation> util = new ExcelUtil<ZcLocation>(ZcLocation.class);
        util.exportExcel(response, list, "资产位置（树）数据");
    }

    /**
     * 获取资产位置（树）详细信息
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:query')")
    @GetMapping(value = "/{lcId}")
    public AjaxResult getInfo(@PathVariable("lcId") Long lcId)
    {
        return success(zcLocationService.selectZcLocationByLcId(lcId));
    }

    /**
     * 新增资产位置（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:add')")
    @Log(title = "资产位置（树）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcLocation zcLocation)
    {
        zcLocation.setCreateBy(getUsername());
        return toAjax(zcLocationService.insertZcLocation(zcLocation));
    }

    /**
     * 修改资产位置（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:edit')")
    @Log(title = "资产位置（树）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ZcLocation zcLocation)
    {
        Long lcId = zcLocation.getLcId();
        if (zcLocation.getParentId().equals(lcId)) {
            return error("修改'" + zcLocation.getLcName() + "'失败，上级分类不能是自己");
        }else if (StringUtils.equals(ZcxtConstants.Location_DISABLE, zcLocation.getStatus()) && zcLocationService.selectNormalChildrenLocationByLcId(lcId) > 0) {
            return error("该分类包含未停用的子分类！");
        }
        zcLocation.setUpdateBy(getUsername());
        return toAjax(zcLocationService.updateZcLocation(zcLocation));
    }

    /**
     * 删除资产位置（树）
     */
    @PreAuthorize("@ss.hasPermi('zcxt:location:remove')")
    @Log(title = "资产位置（树）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lcId}")
    public AjaxResult remove(@PathVariable Long lcId)
    {
        if (zcLocationService.hasChildByLcId(lcId)){
            return warn("存在下级部门，不允许删除");
        }
        return toAjax(zcLocationService.deleteZcLocationByLcId(lcId));
    }
}
